package org.example.service;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * 消费消息的逻辑
 */
@Service
public class MessageConsumerService {

    /**
     * 监听器模式，用于接收来自路由交换机中error等级别的日志消息
     *
     * @param message 接收到的消息
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue("router.error.queue"),
            exchange = @Exchange(value = "routing_exchange", type = "direct"), key = "error_routing_key"))
    public void psubConsumerRouterSMS(Message message) {
        // 从消息中获取消息体的字节数组
        byte[] body = message.getBody();
        // 将字节数组转换为字符串
        String s = new String(body);
        // 打印接收到的error等级别的日志消息
        System.out.println("接收到error等级别日志消息" + s);
    }

    /**
     * 监听器模式，用于接收来自路由交换机中info、error、warning等级别的日志消息
     *
     * @param message 接收到的消息
     */
    @RabbitListener(bindings = @QueueBinding(value =
    @Queue("routing_queue_all"), exchange =
    @Exchange(value = "routing_exchange", type = "direct"),
            key = {"error_routing_key", "info_routing_key", "warning_routing_key"}))
    public void routingConsumerAll(String message) {
        // 打印接收到的info、error、warning等级别的日志消息
        System.out.println("接收到info、error、warning等级别日志消息： " + message);
    }

    /**
     * 监听器模式，用于接收来自主题交换机中符合 info.#.email.# 路由键规则的日志消息
     *
     * @param message 接收到的消息
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic.email.queue"),
            exchange = @Exchange(value = "topic_exchange", type = "topic"), key = "info.#.email.#"))
    public void psubConsumerTopic1(Message message) {
        // 从消息中获取消息体的字节数组
        byte[] body = message.getBody();
        // 将字节数组转换为字符串
        String s = new String(body);
        // 打印接收到的符合规则的日志消息
        System.out.println(" topic 接收到日志消息" + s);
    }

    /**
     * 监听器模式，用于接收来自主题交换机中符合 info.#.sms.# 路由键规则的日志消息
     *
     * @param message 接收到的消息
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic_sms_queue"),
            exchange = @Exchange(value = "topic_exchange", type = "topic"), key = "info.#.sms.#"))
    public void psubConsumerTopic2(Message message) {
        // 从消息中获取消息体的字节数组
        byte[] body = message.getBody();
        // 将字节数组转换为字符串
        String s = new String(body);
        // 打印接收到的符合规则的日志消息
        System.out.println(" topic 接收到日志消息" + s);
    }
}
